<template>
  <div class="question">
      <header class="header">
        <van-nav-bar class="nav-bar"
        title="面试题"
        right-text="答题卡"
        @click-right="show=true"
        ></van-nav-bar>
        <div class="back" @click="back"><van-icon size='22px' name="down"/></div>
      </header>
      <main v-if="!loading" class="test">
        <div class="topic" v-if='question.title'>
          <p class="title">【{{["单选",'多选','简答'][question.type-1]}}题】{{question.title}}</p>
          <div class="fxi" v-if="question.tag">
            <span class="skill" v-for="item in question.tag" :key="item">{{item}}</span>
          </div>
        </div>
        <div class="choice" v-if="question.type==1">
          <div class="select"
          v-for="(v, i) in question.option"
          :key='i'
          @click="choose(alp[i])"
          :class="{checked:seleted.includes(alp[i]),correct:result.singleAnswer===alp[i],wrong:result.singleAnswer!==alp[i]&&result.singleAnswer}"
          >
            <span>{{alp[i]}}.</span>
            <span class="alp">{{v}}</span>
          </div>
        </div>
        <div class="choice" v-if="question.type==2">
          <div class="select"
          v-for="(v, i) in question.option"
          :key='i'
          @click="choose(alp[i])"
          :class="{checked:seleted.includes(alp[i]),correct:(result.multipleAnswer||[]).includes(alp[i]),
          wrong:result.multipleAnswer&& !result.multipleAnswer.includes(alp[i])}"
          >
            <span>{{alp[i]}}.</span>
            <span class="alp">{{v}}</span>
          </div>
        </div>
        <div v-if='showAnswer' class="result">
          <div class="explain">答案解析</div>
          <div class="right-answer">正确答案:{{question.type===1? result.singleAnswer:result.multipleAnswer}}</div>
          <div class="difficult">
            <span>难度:{{result.difficulty}}</span>
            <span>提交次数:{{result.submitNum}}</span>
            <span>正确次数:{{result.correctNum}}</span>
          </div>
          <p>{{result.answerAnalysis}}</p>
        </div>

      </main>
      <footer class="footer">
        <div class="left">
          <div @click="checkLike = !checkLike" :class="{red:checkLike}"><van-icon class="icon" size="22px" name="star-o" /><div>收藏</div></div>
          <div><van-icon class="icon" size="22px" name="edit" @click="showFeedback=true"/><div>反馈</div></div>
        </div>
        <div class="right">
          <span class="answer"><span class="number">{{index+1}}</span>/{{totalList.length}}题</span>
          <van-button v-if='update' :disabled='disabled' @click="submit" class="myButton">提交</van-button>
          <van-button v-else class="next-ques" @click="nextQues">下一题</van-button>
        </div>
      </footer>
      <van-popup v-model="show" position="bottom" :style="{ height: '80%' }">
        <div class="card fxi">
          <div class="sheet">答题卡题</div>
          <span class="answer"><span class="number">{{index+1}}</span>/{{totalList.length}}题</span>
        </div>
        <div class="total fxi">共{{totalList.length}}题</div>
          <ul class="answer-sheet" >
            <li class="option" v-for=" (v,i) in totalList"
            :class="[index===i&&'act', ['correct','wr'][v.state-1]]"
            :key="v.id" @click="quesNum(i)">{{i+1}}</li>
          </ul>
      </van-popup>

    <van-loading v-show="loading" size="40px" vertical>加载中...</van-loading>
    <van-dialog v-model="showFeedback"
    :beforeClose='close' title="意见反馈"
    show-cancel-button confirmButtonText='提交'
    confirmButtonColor="#999"
    >
      <van-field
        v-model="message"
        rows="6"
        autosize
        type="textarea"
        placeholder="请输入你对此问题的反馈意见"
      />
    </van-dialog>
</div>
</template>

<script>
import { Dialog, Toast } from 'vant'
import { questionsDetailAPI, interviewListAPI, feedbackAPI, submitQuesAPI } from '@/api/exercise'
export default {
  name: 'questionDetail',

  data () {
    return {
      index: +(localStorage.getItem('set-index') || 0),
      show: false,
      showDialog: true,
      question: JSON.parse(localStorage.getItem('set-question-detail')) || {},
      totalList: JSON.parse(localStorage.getItem('set-total-list')) || [],
      //  答案解析
      showAnswer: false,
      //  收藏
      checkLike: false,
      //  点击禁止
      disabled: true,
      //  提交
      update: true,
      //  以选择
      seleted: [],
      alp: ['A', 'B', 'C', 'D'],
      option: [],
      loading: false,
      showFeedback: false,
      message: '',
      result: {}
    }
  },
  async created () {
    if (!this.totalList.length) {
      this.loading = true
      const list = await interviewListAPI(this.$route.params)
      // console.log(this.$route.params)
      this.totalList = list.data
      localStorage.setItem('set-total-list', JSON.stringify(this.totalList))
    }
    if (!this.question.title) this.getQues()
  },
  beforeDestroy() {
    localStorage.removeItem('set-question-detail')
    localStorage.removeItem('set-index')
    localStorage.removeItem('set-total-list')
  },
  methods: {
    //  面试题详情
    async getQues () {
      this.question = {}
      this.loading = true
      const res = await questionsDetailAPI(this.totalList[this.index].id)
      console.log(res) // type 1 单选 2 多选 3 简答
      this.question = res.data
      localStorage.setItem('set-question-detail', JSON.stringify(this.question))
      localStorage.setItem('set-index', this.index)
      this.loading = false
    },
    async back () {
      // Dialog({ message: '提示' })

      Dialog.confirm({
        title: '确认退出?',
        message: '下次重新开始刷题哦'
      })
        .then(() => {
          this.$router.push('/library')
        })
        .catch(() => {
        })
    },
    //  选择
    choose (i) {
      this.disabled = false
      if (+this.question.type === 1) {
        this.seleted = [i] // 单选
      } else if (+this.question.type === 2) { // 多选
      //  判断是否存在
        const index = this.seleted.findIndex(v => v === i)
        if (index > -1) this.seleted.splice(index, 1)
        else this.seleted.push(i)
        console.log(this.seleted)
      } else {
        console.log('简答')
      }
    },
    //  提交
    async submit () {
      this.loading = true
      this.update = false
      this.showAnswer = true
      if (+this.question.type === 3) return console.log('简答')
      const choose = +this.question.type === 1 ? { singleAnswer: this.seleted[0] } : { multipleAnswer: this.seleted }
      // console.log(choose)
      const res = await submitQuesAPI({ id: this.totalList[this.index].id, ...choose })
      this.result = res.data
      this.totalList[this.index].state = res.data.isRight ? 1 : 2
      this.loading = false
    },
    //  下一题
    nextQues () {
      // this.getQues(id)
      this.getQues(++this.index)
      localStorage.setItem('set-index', this.index)
      this.change()
    },
    //  答题卡点击事件
    quesNum (i) {
      this.index = i
      this.getQues()
      localStorage.setItem('set-index', this.index)
      this.show = false
      this.change()
    },
    //  修改题目
    change() {
      this.showAnswer = false
      this.update = true
      this.disabled = true
      this.seleted = []
      this.result = {}
    },
    //  反馈
    async close(a, done) {
      if (a === 'confirm') {
        if (!this.message) {
          Toast('请输入内容')
          done(false)
        } else {
          await feedbackAPI({ content: this.message, question: this.totalList[this.index].id })
          Toast('反馈成功')
          done()
        }
        console.log(a)
      } else {
        done()
      }
    }
  }
}
</script>

<style lang='less' scoped>
::v-deep .van-field__control {
  background-color: #f7f5f5;
  border-radius: 10px;
  padding: 10px;
  margin-bottom: 20px;
}
::v-deep .van-loading--vertical{
  width: 100%;
  position: absolute;
  top: 0;
  z-index: 2;
  margin: 50% auto;
}
.myButton {
  width: 100px;
  height: 40px;
  background-color: #ca504e;
  color: #fff;
  border-radius: 5px;
}

.next-ques{
  width: 100px;
  height: 40px;
  color: #fff;
  border-radius: 5px;
  background-color: #4187f2;
}
::v-deep .van-nav-bar__text {
color: #000;
}
::v-deep .van-icon-arrow-left:before{
  font-size: 20px;
  color: #000;
}
.header {
  position: relative;
  .back {
    height: 100%;
    width: 50px;
    position: absolute;
    top: 0;
    z-index: 1;
    display: flex;
    justify-content: center;
    align-items: center;
    transform: rotateZ(90deg);
  }
}
.nav-bar{
  box-shadow: 0 3px 5px rgb(100 101 102 / 30%);

 }
.test {
  padding: 20px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  .topic {
    .fxi {
      flex-wrap: wrap;
      padding: 15px 0;
    }
    span {
      border-radius: 3px;
      padding: 2px 10px;
      display: inline-block;
      background-color: #f6f4f5;
      color: #bebec5;
      font-size: 12px;
      margin: 5px 0;
    }
    .skill {
      margin-right: 10px;
    }
  }
  .choice {
    height: 350px;

    .select {
      border-radius: 5px;
      border:1px solid #f6f4f5;
      padding: 15px;
      color: #000;
      margin-bottom: 20px;
      .alp {
        margin-left: 8px;
      }
    }
  }
  .result {
    border-top:1px solid #f6f4f5;
    padding:20px 0;
    >p {
      padding-bottom: 60px;
    }
    .explain{
    font-size: 18px;
    font-weight: 700;
    margin-bottom:15px;
    }
    .right-answer {
      color:#5ec480 ;
      margin-bottom:15px;
    }
    .difficult {
      padding: 0 10px;
      display: flex;
      justify-content: space-between;
      align-items: center;
      font-size: 12px;
      color: rgb(112, 110, 110);
      height: 35px;
      background-color:#f6f4f5;
      margin-bottom: 15px;
    }

  }
}

.footer {
  width: 100%;
  height: 70px;
  border-top: 1px solid rgb(236, 235, 235);
  position: fixed;
  bottom: 0;
  padding: 15px;
  display: flex;
  justify-content: space-between;
  background-color: #fff;
  .left {
    width: 100px;
    height: 100%;
    font-size: 14px;
    color: #b4b3bd;
    display: flex;
    justify-content: space-around;

    .red{
      color:#ca504e ;
    }

      div {
        display: flex;
        flex-direction: column;
        align-items: center;
    }

  }
  .right {
    display: flex;
    flex: 1;
    .answer {
      display: inline-block;
      text-align: center;
      line-height:40px ;
      flex: 1;
    }
    .number{
      color: #ca504e;
    }
  }
}
.card {
  height: 60px;
  padding: 0 10px;
  .sheet {
    flex: 1;
    margin-left: 80px;
    text-align: center;
    font-size: 16px;
    font-weight: 700;
  }
    .answer {
      width: 80px;
      line-height:40px ;
    }
    .number{
      color: #ca504e;
    }

}

.total {
  height: 50px;
  background-color: #f6f4f5;
  font-size: 14px;
  padding-left: 20px;
  color: #b4b5bc;
}
.answer-sheet {
  padding: 35px;
  background-color: #fff;
  display: flex;
  flex-wrap: wrap;
  // justify-content: space-between;
  .option {
    width: 32px;
    height: 32px;
    margin: 8px;
    border: 1px solid #bebec5;
    border-radius: 50%;
    text-align: center;
    line-height: 30px;
    color: #bebec5;
  }
}

.test .choice .select, .answer-sheet .option {
  &.checked {
    margin-bottom: 20px;
    border:1px solid #bebec5;
    color: #b4b3bd;
    &.wrong {
      background-color: #fcf0eb;
      color: #ed615b;
      border: none;
    }
  }
  &.act {
    border:1px solid #000;
    color: #000;
  }
  &.wr {
    background-color: #fcf0eb;
    color: #ed615b;
    border: none;
  }
  &.correct {
    background-color: #e3f9dc;
    color: #7cd095;
    border: none;
  }
}
.fxi {
  display: flex;
  align-items: center;
}
</style>
